package com.chalk.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.model.Coach;
import com.chalk.dao.CoachDao;
import com.chalk.service.CoachAndSeService;
import com.chalk.service.CoachService;

import com.chalk.util.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author glq
 * @since 2020-12-21
 */

@Service
public class CoachServiceImpl extends ServiceImpl<CoachDao, Coach> implements CoachService {

    @Autowired
    CoachAndSeService coachAndSeService;

    @Override
    public int addCoach(Coach entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int updateCoach(Coach entity) {
        return baseMapper.updateById(entity);
    }


    @Override
    public int deleteCoach(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public Page<Coach> getCoachForPage(Map<String, Object> map) {
        String seId = (String) map.get("seId");
        //前端排序字段
        String orderBy = (String) map.get("orderBy");
        //降序字段
        List<String> desc = new ArrayList<>();
        if (StringUtils.isBlank(orderBy)) {
            desc.add("c.create_time");
        } else {
            if (orderBy.equals("grade_value")) {
                desc.add("g." + orderBy);
            } else {
                desc.add("c." + orderBy);
            }
        }
        Page<Coach> coachPage = new Page<>(MapUtil.getPage(map), MapUtil.getSize(map));
        coachPage.setDescs(desc);
        if (StringUtils.isNotBlank(seId)) {
            List<Integer> ids = coachAndSeService.getCoachIdsBySeId(Integer.valueOf(seId));
            if (ids.size() == 0) {
                return coachPage;
            } else {
                map.put("ids", ids);
            }
        }
        return coachPage.setRecords(baseMapper.getCoachForPage(coachPage, map));
    }
}
